package com.example.archive.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.archive.entity.Notice;
import com.example.archive.mapper.NoticeMapper;
import com.example.archive.service.NoticeService;
import com.example.archive.service.SystemLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {
    
    @Autowired
    private SystemLogService systemLogService;
    
    @Override
    public List<Notice> getPublishedNotices() {
        return baseMapper.selectPublishedNotices();
    }
    
    @Override
    public Page<Notice> getNoticesWithPublisher(int current, int size) {
        Page<Notice> page = new Page<>(current, size);
        return baseMapper.selectNoticesWithPublisher(page);
    }
    
    @Override
    public boolean publishNotice(Notice notice) {
        notice.setPublishTime(LocalDateTime.now());
        // 如果没有设置状态，默认为published
        if (notice.getStatus() == null || notice.getStatus().isEmpty()) {
            notice.setStatus("published");
        }
        boolean result = save(notice);
        
        if (result) {
            String action = "published".equals(notice.getStatus()) ? "发布公告" : "提交公告";
            systemLogService.recordLog(notice.getPublisherId(), action, 
                action + "：" + notice.getTitle());
        }
        
        return result;
    }
    
    @Override
    public boolean approveNotice(Integer noticeId, String status) {
        Notice notice = getById(noticeId);
        if (notice != null) {
            notice.setStatus(status);
            boolean result = updateById(notice);
            
            if (result) {
                systemLogService.recordLog(null, "审核公告", 
                    "审核公告：" + notice.getTitle() + "，状态：" + status);
            }
            
            return result;
        }
        return false;
    }
} 